/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.command;

import static java.lang.Math.max;
import static java.lang.Math.min;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.InteractionOperand;

import com.cea.papyrus.diagraminterchange2.di2.Diagram;

/**
 * Determines the preferred bounds for a continuation. Continuations can only
 * live exclusively inside an interaction operand.
 * <p>
 * The following attributes must be set before the command can run successfully:
 * </p>
 * <ul>
 * <li>Hint - in absolute coords
 * <li>Operand
 * <li>Diagram <li<AnyEditPart
 * </ul>
 * <p>
 * When completed, the following attributes can be read to access the result:
 * </p>
 * <ul>
 * <li>PreferredBounds - in absolute coords
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class DecideContinuationBoundsCommand extends Command {

	private static final int HEIGHT = 30;
	private static final int DEFAULT_WIDTH = 150;
	private static final int MIN_WIDTH = 90; // A little bit smaller than
												// minimum CF
	// width

	private Rectangle hint;
	private Diagram diagram;
	private InteractionOperand operand;
	private EditPart anyEditPart;
	private Rectangle preferred;

	public void setDiagram(final Diagram diagram) {
		this.diagram = diagram;
	}

	public void setHint(final Rectangle hint) {
		this.hint = hint.getCopy();
	}

	public void setOperand(final InteractionOperand operand) {
		this.operand = operand;
	}

	public void setAnyEditPart(final EditPart editPart) {
		this.anyEditPart = editPart;
	}

	public Rectangle getPreferredBounds() {
		return preferred;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void execute() {
		final Rectangle operandBounds = OperandUtils.getOperandBounds(diagram,
				operand, anyEditPart);

		// TODO if operandBounds.height < HEIGHT, the diagram must be vertically
		// expanded

		// Horizontally: must fully fit within the owner
		int left = max(operandBounds.x + 2, hint.x);

		int width = min(operandBounds.width - 2, hint.width);
		if (width == 0)
			width = between(MIN_WIDTH, operandBounds.right() + 10 - left,
					DEFAULT_WIDTH);

		if (left + width >= operandBounds.right() - 2) {
			width = operandBounds.right() - 2 - left;
			if (width < MIN_WIDTH) {
				int diff = MIN_WIDTH - width;
				diff = min(diff, left - operandBounds.x + 2);
				left -= diff;
				width += diff;
			}
		}

		// Vertically: must fully fit within the owner
		int top = max(operandBounds.y + 2, hint.y);
		if (top > operandBounds.bottom() - 2 - HEIGHT)
			top = max(operandBounds.y + 1, operandBounds.bottom() - 2 - HEIGHT);

		preferred = new Rectangle(left, top, width, HEIGHT);
	}

	/*
	 * private Rectangle getOperandBounds() { Rectangle bounds = ; /*Object
	 * cfragmentView = diagram.getViewFor((NamedElement)operand.getOwner());
	 * GraphicalEditPart cfragmentEP = (GraphicalEditPart)
	 * anyEditPart.getViewer().getEditPartRegistry().get(cfragmentView);
	 * cfragmentEP.getFigure().translateToAbsolute(bounds); * / return bounds; }
	 */

	@Override
	public void redo() {
		execute();
	}

	@Override
	public void dispose() {
		super.dispose();
		anyEditPart = null;
		operand = null;
		diagram = null;
		hint = null;
		preferred = null;
	}

	/**
	 * Compares the given value to the specified min and max values and returns:
	 * <ul>
	 * <li>min if value &lt; min
	 * <li>value if min &lt; value &lt; max
	 * <li>max if value &gt; max
	 * </ul>
	 */
	private int between(int min, int value, int max) {
		return value < min ? min : (value < max ? value : max);
	}
}
